#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : vendor
# @IDE            : PyCharm
# @desc           : 描述

from typing import List, Optional, Union
from core.data_types import DatetimeStr
from pydantic import BaseModel, ConfigDict, Field, root_validator


class ASRFeature(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    characteristic: str
    dimension: str
    irr: Optional[str]
    grade: Optional[str]
    count: Optional[str]


class ASRContent(ASRFeature):
    model_config = ConfigDict(from_attributes=True)

    id: Optional[int] = Field(None, exclude=True)

    asr_id: int

    insp_lot_no: Optional[str] = None
    lot_size: Optional[int] = None

    sample_size: Optional[int] = None
    accept_qty: Optional[int] = None
    reject_qty: Optional[int] = None

    reject_tag_no: Optional[str] = None

    insp_stamp: Optional[str] = None

    create_user: Optional[str] = None


class ASRContentSimpleOut(ASRContent):
    model_config = ConfigDict(from_attributes=True)

    id: int


class ASR(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    asr_no: str
    part_no: str
    qar_no: str

    product_type: str
    part_type: str
    form_type: str

    status: Optional[str] = None

    po_fo_no: Optional[str] = None
    packing_sheet_no: Optional[str] = None
    mfg_shop_no: Optional[str] = None

    plan_remark: Optional[str] = None
    insp_remark: Optional[str] = None

    plan_user: Optional[str] = None
    insp_user: Optional[str] = None

    ok_to_file: Optional[str] = None

    create_user: Optional[str] = None

    insp_result: Optional[str] = None


class ASRSimpleOut(ASR):
    model_config = ConfigDict(from_attributes=True)

    id: int

    erratum_attachment: Optional[str] = None
    erratum_user: Optional[str] = None

    erratum_datetime: Optional[DatetimeStr] = None

    update_datetime: Optional[DatetimeStr] = None
    create_datetime: Optional[DatetimeStr] = None


class ASROut(ASRSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    contents: List[ASRContentSimpleOut] = Field([])
    delete_contents: Optional[List[ASRContentSimpleOut]] = []

    @root_validator(pre=True)
    def convert(cls, v):
        delete_contents = [i for i in v.contents if i.is_delete]
        contents = [i for i in v.contents if not i.is_delete]
        data = ASRSimpleOut.model_validate(v).model_dump()
        return dict(delete_contents=delete_contents, contents=contents, **data)
    

class ASRIn(ASR):
    contents: Optional[List[ASRContent]] = Field([], exclude=True)

    features: Optional[List[ASRFeature]] = Field([], exclude=True)

    quantity: Optional[Union[str, int]] = Field(None, exclude=True) # 来料数
    lot_batch_no: Optional[str] = Field(None, exclude=True)  # 炉批号


class ASRUpdateStatus(BaseModel):
    status: Optional[str] = None


class ASRUpdateErratum(BaseModel):
    erratum_attachment: Optional[str] = None
    erratum_datetime: Optional[DatetimeStr] = None
    erratum_user: Optional[str] = None


class ASRStampType(BaseModel):
    type: str